using System;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;

namespace FireStarterModeller.BO
{
    public class DMObjectIdentityAllPropsCompulsoryRule : IBusinessObjectRule
    {
        private DMObjectIdentity _dmObjectIdentity;

//        public DMObjectIdentityAllPropsCompulsoryRule(DMObjectIdentity dmObjectIdentity)
//        {
//            if (dmObjectIdentity == null) throw new ArgumentNullException("dmObjectIdentity");
//            _dmObjectIdentity = dmObjectIdentity;
//        }

        #region Implementation of IBusinessObjectRule

        /// <summary>
        /// Indicates whether the property value is valid against the rules
        /// </summary>
        /// <returns>
        /// Returns true if valid
        /// </returns>
        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _dmObjectIdentity = (DMObjectIdentity) bo;
            BusinessObjectCollection<DMProperty> objectIdProps = _dmObjectIdentity.Properties;
            if (objectIdProps.Count == 1)
            {
                if (objectIdProps[0].AutoIncrementing.GetValueOrDefault()) return true;
            }
            foreach (DMProperty property in objectIdProps)
            {
                if (property.Compulsory.GetValueOrDefault() == false) return false;
            }
            return true;
        }

        /// <summary>
        /// Returns the rule name
        /// </summary>
        public string Name
        {
            get { return "DMObjectIdentityAllPropsCompulsory"; }
        }

        /// <summary>
        /// Returns the error message for if the rule fails.
        /// </summary>
        public string Message
        {
            get { return "All the properties that form the DMObjectIdentity for Entity/Class '" + _dmObjectIdentity.Class + "' must be compulsory."; }
        }
        /// <summary>
        /// The <see cref="P:Habanero.Base.IBusinessObjectRule.ErrorLevel" /> for this BusinessObjectRule e.g. Warning, Error. 
        /// </summary>
        public ErrorLevel ErrorLevel
        {
            get { return Habanero.Base.ErrorLevel.Error; }
        }

        #endregion
    }
}